package main

import "fmt"

//实现go多态，需要实现定义接口

// IAttack 定一个接口，注意类型是interface
type IAttack interface {
	// Attack 接口函数可以有多个，但是只能有函数原型，不可以有实现
	Attack()
}

// DoAttack 定义一个多态的调用接口，传入不同的对象，调用不同的接口，实现不同效果
func DoAttack(ptr IAttack) {
	ptr.Attack()
}

// HumanLowLevel 低等级
type HumanLowLevel struct {
	name  string
	level int
}

func (a *HumanLowLevel) Attack() {
	fmt.Println("我是:", a.name, "等级为: ", a.level)
}

type HumanHighLevel struct {
	name  string
	level int
}

func (a *HumanHighLevel) Attack() {
	fmt.Println("我是:", a.name, "等级为: ", a.level)
}

func main() {
	// 定义一个包含Attack接口变量,任何一个实现IAttack::Attack() 都可以赋值给player
	var player IAttack

	lowLevel := HumanLowLevel{
		name:  "cc",
		level: 1,
	}

	highLever := HumanHighLevel{
		name:  "Mrs",
		level: 99,
	}

	lowLevel.Attack()
	highLever.Attack()

	//对player赋值为lowLever，接口可接收任意类型
	player = &lowLevel
	player.Attack()

	player = &highLever
	player.Attack()

	fmt.Println("polymorphism...")
	DoAttack(&lowLevel)
	DoAttack(&highLever)
}
